package com.example.safeassistan.train.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.train.adapter.PaperResultSingleAdapter;
import com.example.safeassistan.train.model.PaperResultModel;
import com.example.safeassistan.utile.JsonUtils;

import org.json.JSONObject;

import java.io.Serializable;

/***
 * 考试结果
 * **/

public class ActivityTrainResult extends BaseActivity {
    private RecyclerView rcSignal,rcMuti,rcJduge;
    private PaperResultSingleAdapter singleAdapter,multAdapter,jdugeAdapter;
    private TextView tvMyScore,tvTotalScore,tvUseTime,
            tvItemCount,tvCorrectCount,tvCreateTime,
            tvResult;
    private TextView tvDateTitle,tvResultTitle;
    private TextView tvMutil,tvJudge;
    private int type=1; //1 模拟 2 考试
    private String paperId="";//试卷id
    private LinearLayout llBottom;
    private  PaperResultModel model=null;

    private ImageView _barBack;


    public static void actionStart(Context context,int type,String paperId,PaperResultModel model) {
        Intent intent = new Intent(context, ActivityTrainResult.class);
        intent.putExtra("type",type);
        intent.putExtra("paperId",paperId);
        intent.putExtra("model",(Serializable) model);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_train_result;
    }

    @Override
    protected void initView() {
        setTitleText("考试结果");
        _barBack=findViewById(R.id._barBack);
        type=getIntent().getIntExtra("type",1);
        paperId=getIntent().getStringExtra("paperId");
        model=(PaperResultModel)getIntent().getSerializableExtra("model");
        llBottom=findViewById(R.id.llBottom);
        rcJduge=findViewById(R.id.rcJduge);
        rcSignal=findViewById(R.id.rcSignal);
        rcMuti=findViewById(R.id.rcMuti);
        tvMutil=findViewById(R.id.tvMutil);
        tvJudge=findViewById(R.id.tvJudge);
        tvDateTitle=findViewById(R.id.tvDateTitle);
        tvResultTitle=findViewById(R.id.tvResultTitle);
        tvMyScore=findViewById(R.id.tvMyScore);
        tvTotalScore=findViewById(R.id.tvTotalScore);
        tvUseTime=findViewById(R.id.tvUseTime);
        tvItemCount=findViewById(R.id.tvItemCount);
        tvCorrectCount=findViewById(R.id.tvCorrectCount);
        tvCreateTime=findViewById(R.id.tvCreateTime);
        tvResult=findViewById(R.id.tvResult);
        _barBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type==2){
                    TrainExamList.actionStart(ActivityTrainResult.this);
                }else {
                    TrainSimulateList.actionStart(ActivityTrainResult.this);
                }

            }
        });
        switch (type){
            case 1:
                tvDateTitle.setText("本次模拟日期");
                tvResultTitle.setText("本次模拟结果");
                llBottom.setVisibility(View.GONE);
                break;
            case 2:
                tvDateTitle.setText("考试日期");
                tvResultTitle.setText("考试结果");
                llBottom.setVisibility(View.GONE);
                break;
        }

        if(model==null){
            getData();
        }else {
            setView();
        }
    }

    @Override
    protected void initData() { }

    public void getData(){
        new HttpRequest(this).doGet(UrlConstants.getExamResult+paperId, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        Object obj = JsonUtils.fromJson(result.toString(), PaperResultModel.class);
                        if (obj instanceof PaperResultModel) {
                             model=(PaperResultModel) obj;
                            setView();
                        }
                    }else {
                        Toast.makeText(ActivityTrainResult.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    public void setView(){
        tvMyScore.setText(model.getResult().getPaperMark()+"");
        tvTotalScore.setText("/"+model.getResult().getTotalStore());
        tvUseTime.setText(model.getResult().getUseTime());
        tvItemCount.setText(model.getResult().getKnowNum()+"");
        tvCorrectCount.setText(model.getResult().getTrueNum()+"");
        tvCreateTime.setText(model.getResult().getSubmissionTime());
        tvResult.setText(model.getResult().getPaperResult());

        if (model.getResult().getSingleSheet().size()>0){
            singleAdapter=new PaperResultSingleAdapter(model.getResult().getSingleSheet());
            rcSignal.setAdapter(singleAdapter);
            rcSignal.setLayoutManager(new GridLayoutManager(getContext(),5));
        }

        if (model.getResult().getManySheet().size()>0){
            tvMutil.setVisibility(View.VISIBLE);
            multAdapter=new PaperResultSingleAdapter(model.getResult().getManySheet());
            rcMuti.setAdapter(multAdapter);
            rcMuti.setLayoutManager(new GridLayoutManager(getContext(),5));
        }else {
            tvMutil.setVisibility(View.GONE);
        }

        if (model.getResult().getJudgeSheet().size()>0){
            tvJudge.setVisibility(View.VISIBLE);
            jdugeAdapter=new PaperResultSingleAdapter(model.getResult().getJudgeSheet());
            rcJduge.setAdapter(jdugeAdapter);
            rcJduge.setLayoutManager(new GridLayoutManager(getContext(),5));
        }else {
            tvJudge.setVisibility(View.GONE);
        }
    }
}
